package com.lineage.server.serverpackets;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Party;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class S_PacketBoxParty extends ServerBasePacket {
  private static final Log _log = LogFactory.getLog(S_PacketBoxParty.class);
  
  public static final int MSG_PARTY1 = 104;
  
  public static final int MSG_PARTY2 = 105;
  
  public static final int MSG_PARTY3 = 110;
  
  public static final int MSG_LEADER = 106;
  
  private byte[] _byte;
  
  public S_PacketBoxParty(L1PcInstance pc, L1Party party) {
    this._byte = null;
    try {
      HashMap<Integer, L1PcInstance> map = new HashMap<>();
      map.putAll(party.partyUsers());
      writeC(250);
      writeC(104);
      writeC(map.size() - 1);
      Iterator<Integer> iterator = map.keySet().iterator();
      while (iterator.hasNext()) {
        Integer key = iterator.next();
        L1PcInstance tgpc = map.get(key);
        if (tgpc == null)
          continue; 
        if (pc.equals(tgpc))
          continue; 
        writeD(tgpc.getId());
        writeS(tgpc.getName());
        double nowhp = tgpc.getCurrentHp();
        double maxhp = tgpc.getMaxHp();
        writeC((int)(nowhp / maxhp * 100.0D));
        writeD(tgpc.getMapId());
        writeH(tgpc.getX());
        writeH(tgpc.getY());
      } 
      map.clear();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public S_PacketBoxParty(L1Party party, L1PcInstance pc) {
    this._byte = null;
    try {
      HashMap<Integer, L1PcInstance> map = new HashMap<>();
      map.putAll(party.partyUsers());
      writeC(250);
      writeC(110);
      writeC(map.size() - 1);
      Iterator<Integer> iterator = map.keySet().iterator();
      while (iterator.hasNext()) {
        Integer key = iterator.next();
        L1PcInstance tgpc = map.get(key);
        if (tgpc == null)
          continue; 
        if (pc.equals(tgpc))
          continue; 
        writeD(tgpc.getId());
        writeD(tgpc.getMapId());
        writeH(tgpc.getX());
        writeH(tgpc.getY());
      } 
      map.clear();
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public S_PacketBoxParty(L1PcInstance tgpc) {
    this._byte = null;
    try {
      writeC(250);
      writeC(105);
      writeD(tgpc.getId());
      writeS(tgpc.getName());
      double nowhp = tgpc.getCurrentHp();
      double maxhp = tgpc.getMaxHp();
      writeC((int)(nowhp / maxhp * 100.0D));
      writeD(tgpc.getMapId());
      writeH(tgpc.getX());
      writeH(tgpc.getY());
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public S_PacketBoxParty(int objid, String name) {
    this._byte = null;
    try {
      writeC(250);
      writeC(106);
      writeD(objid);
      writeS(name);
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
